<?php

class BookManager extends Manager
{
	const TABLE_NAME = 'ksiazki';
	const RELATIONTABLE_BOOKS_NAME = 'ksiazkakategoria';
	const OBJECT_NAME = 'Book';
	const PK_NAME = 'id';

	public function getById($id)
	{
		$select = $this->_db->select()->from(self::TABLE_NAME)
					->where('id = ?', $id);
		$result = $this->_db->fetchRow($select);
		return $this->_createObject(self::OBJECT_NAME, $result);
	}
	
	public function getByCategoryId($id)
	{
		$select = $this->_db->select()->from(self::TABLE_NAME, self::TABLE_NAME . '.id')
					->join(self::RELATIONTABLE_BOOKS_NAME, 
						self::RELATIONTABLE_BOOKS_NAME . '.idKsiazki = ' . self::TABLE_NAME . '.id', '')
					->join(CategoryManager::TABLE_NAME, 
						self::RELATIONTABLE_BOOKS_NAME . '.idKategorii = ' . CategoryManager::TABLE_NAME . '.id', '')
					->where(CategoryManager::TABLE_NAME . '.id = ?', $id);
					
		$result = $this->_db->fetchAll($select);
		return $this->_createCollection(self::OBJECT_NAME, $result);
	}
	
	public function addBookToCategory(Book $book, Category $category)
	{
		$bind = array('idKsiazki' => $book->id, 'idKategorii' => $category->id);
		$this->_db->insert(self::RELATIONTABLE_BOOKS_NAME, $bind);
	}
}